package org.sparrow.common.service;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.sparrow.common.entity.Output;
import org.sparrow.common.entity.User;
import org.sparrow.common.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service // @Service 注解 用于标注业务逻辑
public class UserService {
  UserMapper userMapper;

  @Autowired
  UserService(UserMapper userMapper) {
    this.userMapper = userMapper;
  }

  // 创建输出实体类
  Output output = new Output();

  // 创建模块实体类
  User user = new User();

  /**
   * 查找用户结果
   *
   * @param username 用户名
   * @param password 密码
   * @param realName 真实姓名
   * @param gender 性别
   * @param birthday 生日
   * @return 返回结果
   */
  User FindUserResult(
      String username, String password, String realName, Integer gender, String birthday) {
    if (gender == null) {
      gender = 0;
    }

    user.setUsername(username);
    user.setPassword(password);
    user.setRealName(realName);
    user.setGender(gender);
    user.setBirthday(birthday);

    return userMapper.FindUser(user);
  }

  /**
   * 过滤用户列表
   *
   * @param username 用户名
   * @param gender 性别
   * @param pageNum 当前页码
   * @param pageSize 每页显示数量
   * @return 返回结果
   */
  public String FilterUser(String username, Integer gender, int pageNum, int pageSize) {
    try {
      if (gender == null) {
        gender = -1;
      }

      user.setUsername(username);
      user.setGender(gender);

      // 分页查询
      PageHelper.startPage(pageNum, pageSize); // pageNum: 当前页码, pageSize: 每页显示数量
      PageInfo<User> pageInfo = new PageInfo<>(userMapper.FilterUser(user));

      Map<String, Object> data = new HashMap<>();

      data.put("list", pageInfo.getList());
      data.put("total", pageInfo.getTotal());

      // 普通查询
      // List<User> list;
      // list = userMapper.FilterUser();

      // data.put("list", list);
      // data.put("total", list.size());

      if (pageInfo.getTotal() == 0) {
        output.setData(data);
        output.setCode(20002);
        output.setMsg("暂无数据");
      } else {
        output.setData(data);
        output.setCode(0);
        output.setMsg("查询数据列表成功");
      }
    } catch (Exception e) {
      output.setCode(50000);
      output.setMsg("error");
    }
    return JSON.toJSONString(output);
  }

  /**
   * 查询用户
   *
   * @param username 用户名
   * @return 返回结果
   */
  public String FindUser(String username) {
    try {
      user.setUsername(username);

      User data = userMapper.FindUser(user);
      if (data == null) {
        output.setData("");
        output.setCode(20002);
        output.setMsg("暂无数据");
      } else {
        output.setData(data);
        output.setCode(0);
        output.setMsg("查询数据成功");
      }
    } catch (Exception e) {
      output.setCode(50000);
      output.setMsg("error");
    }
    return JSON.toJSONString(output);
  }

  /**
   * 创建用户
   *
   * @param username 用户名
   * @param password 密码
   * @param realName 真实姓名
   * @param gender 性别
   * @param birthday 生日
   * @return 返回结果
   */
  public String CreateUser(
      String username, String password, String realName, Integer gender, String birthday) {
    try {
      User data = FindUserResult(username, password, realName, gender, birthday);

      output.setData(null);
      output.setCode(0);
      if (data == null) {
        userMapper.CreateUser(user);
        output.setMsg("创建数据成功");
      } else {
        output.setCode(20001);
        output.setMsg("数据已存在");
      }
    } catch (Exception e) {
      output.setCode(50000);
      output.setMsg("error");
    }
    return JSON.toJSONString(output);
  }

  /**
   * 删除用户
   *
   * @param username 用户名
   * @return 返回结果
   */
  public String DeleteUser(String username) {
    try {
      user.setUsername(username);

      User data = userMapper.FindUser(user);

      output.setData(null);
      output.setCode(0);
      if (data == null) {
        output.setCode(20002);
        output.setMsg("数据已删除");
      } else {
        userMapper.DeleteUser(user);
        output.setMsg("删除数据成功");
      }
    } catch (Exception e) {
      output.setCode(50000);
      output.setMsg("error");
    }
    return JSON.toJSONString(output);
  }

  /**
   * 更新用户
   *
   * @param username 用户名
   * @param password 密码
   * @param realName 真实姓名
   * @param gender 性别
   * @param birthday 生日
   * @return 返回结果
   */
  public String UpdateUser(
      String username, String password, String realName, Integer gender, String birthday) {
    try {
      User data = FindUserResult(username, password, realName, gender, birthday);

      output.setData(null);
      output.setCode(0);
      if (data == null) {
        output.setCode(20002);
        output.setMsg("数据已删除");
      } else {
        userMapper.UpdateUser(user);
        output.setMsg("更新数据成功");
      }
    } catch (Exception e) {
      output.setCode(50000);
      output.setMsg("error");
    }
    return JSON.toJSONString(output);
  }
}
